<template>
  <el-dialog
    :title="!dataForm.menuId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="服务名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="服务名称"></el-input>
    </el-form-item>
    <el-form-item label="上级服务id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="上级服务id"></el-input>
    </el-form-item>
    <el-form-item label="服务层级" prop="level">
      <el-input v-model="dataForm.level" placeholder="服务层级"></el-input>
    </el-form-item>
    <el-form-item label="服务排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="服务排序"></el-input>
    </el-form-item>
    <el-form-item label="是否显示" prop="showStatus">
      <el-input v-model="dataForm.showStatus" placeholder="是否显示"></el-input>
    </el-form-item>
    <el-form-item label="集群数" prop="colony">
      <el-input v-model="dataForm.colony" placeholder="集群数"></el-input>
    </el-form-item>
    <el-form-item label="实例数" prop="example">
      <el-input v-model="dataForm.example" placeholder="实例数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          menuId: 0,
          name: '',
          parentId: '',
          level: '',
          sort: '',
          showStatus: '',
          colony: '',
          example: ''
        },
        dataRule: {
          name: [
            { required: true, message: '服务名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '上级服务id不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '服务层级不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '服务排序不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '是否显示不能为空', trigger: 'blur' }
          ],
          colony: [
            { required: true, message: '集群数不能为空', trigger: 'blur' }
          ],
          example: [
            { required: true, message: '实例数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.menuId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.menuId) {
            this.$http({
              url: this.$http.adornUrl(`/service_controller/servicecontroller/info/${this.dataForm.menuId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.serviceController.name
                this.dataForm.parentId = data.serviceController.parentId
                this.dataForm.level = data.serviceController.level
                this.dataForm.sort = data.serviceController.sort
                this.dataForm.showStatus = data.serviceController.showStatus
                this.dataForm.colony = data.serviceController.colony
                this.dataForm.example = data.serviceController.example
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/service_controller/servicecontroller/${!this.dataForm.menuId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'menuId': this.dataForm.menuId || undefined,
                'name': this.dataForm.name,
                'parentId': this.dataForm.parentId,
                'level': this.dataForm.level,
                'sort': this.dataForm.sort,
                'showStatus': this.dataForm.showStatus,
                'colony': this.dataForm.colony,
                'example': this.dataForm.example
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
